package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.tool.utils.Func;
import com.atguigu.common.tool.utils.StringPool;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuImagesEntity;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimall.product.service.*;
import com.atguigu.gulimall.product.vo.SkuItemSaleAttrVO;
import com.atguigu.gulimall.product.vo.SkuItemVO;
import com.atguigu.gulimall.product.vo.SpuItemAttrGroupVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author 17495
 */
@Service("skuInfoService")
@AllArgsConstructor
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    private final SkuImagesService skuImagesService;

    private final SpuInfoDescService spuInfoDescService;

    private final AttrGroupService attrGroupService;

    private final SkuSaleAttrValueService skuSaleAttrValueService;

    private final ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        String key = (String) params.get("key");
        String catelogId = (String) params.get("catelogId");
        String brandId = (String) params.get("brandId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");

        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params),
                Wrappers.<SkuInfoEntity>lambdaQuery()
                        .and(Func.isNotEmpty(key), i -> i.eq(SkuInfoEntity::getSkuId, key)
                                .or()
                                .like(SkuInfoEntity::getSkuName, key))
                        .eq(Func.isNotEmpty(catelogId) && !StringPool.ZERO.equalsIgnoreCase(catelogId), SkuInfoEntity::getCatalogId, catelogId)
                        .eq(Func.isNotEmpty(brandId) && !StringPool.ZERO.equalsIgnoreCase(brandId), SkuInfoEntity::getBrandId, brandId)
                        .ge(SkuInfoEntity::getPrice, min)
                        .le(Func.isNotEmpty(max) && new BigDecimal(max).compareTo(new BigDecimal(StringPool.ZERO)) > 0, SkuInfoEntity::getPrice, max)
        );
        return new PageUtils(page);
    }

    @Override
    public SkuItemVO item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVO skuItemVo = new SkuItemVO();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            // 1. sku基本信息获取 pms_sku_info
            SkuInfoEntity info = getById(skuId);
            skuItemVo.setInfo(info);
            return info;
        }, executor);

        CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync(res -> {
            // 3. 获取spu的销售属性组合
            List<SkuItemSaleAttrVO> saleAttrVoS = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrVoS);
        }, executor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync(res -> {
            // 4. 获取spu的介绍 pms_spu_info_desc
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDescEntity(spuInfoDescEntity);
        }, executor);

        CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync(res -> {
            // 5. 获取spu的规格参数信息
            List<SpuItemAttrGroupVO> groupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(groupAttrs);
        }, executor);

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            // 2. sku的图片信息 pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.list(Wrappers.<SkuImagesEntity>lambdaQuery()
                    .eq(SkuImagesEntity::getSkuId, skuId));
            skuItemVo.setImages(images);
        }, executor);

        // 等待所有任务都完成
        CompletableFuture.allOf(saleAttrFuture, descFuture, baseAttrFuture, imageFuture).get();

        return skuItemVo;
    }

}
